<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
       <h3>Book Detail Page</h3>
       Book: <%=request.getParameter("book") %>
       <br><br>
       <a href="books.jsp">return</a>
       <%
         String bookName=request.getParameter("book");
         Cookie[] cookies=request.getCookies();
         Cookie temp=null;
         List<Cookie> list=new ArrayList<Cookie>();
         if(cookies!=null&&cookies.length>0){
        	 for(Cookie c:cookies){
        		 if(c.getName().startsWith("BOOK_NAME")){
        			 if(c.getValue().equals(bookName)){
    					 temp=c;
    				 }
        			 if(list.size()<5){
        				 list.add(c);
        				 
        			 }else{
        				 if(temp==null){
        					 temp=list.get(0);
        				 }
        			 }
        		 }
        	 }
         }
         if(temp!=null){
        	 temp.setMaxAge(0);
        	 response.addCookie(temp);
         }
         Cookie cookie=new Cookie("BOOK_NAME"+bookName,bookName);
         response.addCookie(cookie);
       %>
</body>
</html>